package cn.kgc.ssm.controller;

import cn.hutool.core.util.IdUtil;
import cn.kgc.ssm.entity.Slide;
import cn.kgc.ssm.response.ResponseResult;
import cn.kgc.ssm.service.SlideService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author 课工场
 * @date 2024/11/20
 * @description 轮播区的管理
 */
@RestController
@RequestMapping("slide")
public class SlideController {


    @Autowired
    private SlideService slideService;

    // 分页
    @GetMapping("page")
    public ResponseResult slidePage(Integer page, Integer limit) {

        return slideService.slidePage(page, limit);

    }

    // 图片上传
    @PostMapping("upload")
    public ResponseResult uploadImage(MultipartFile file) {

        //  1. 获取文件的名字   xxx.jpg
        String originalFilename = file.getOriginalFilename();
        // 2. 获取文件的扩展名   jpg
        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);
        // 3. 生成uuid  作为文件的名字
        String uuid = IdUtil.simpleUUID();
        // 4. 拼接文件名
        String lastName = uuid + "." + filenameExtension;
        // 5.上传图片
        File f = new File("D:/upload/slides/" + lastName);
        try {
            file.transferTo(f);
            return ResponseResult.success().data(lastName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    // 添加图片资源信息
    @PostMapping("uploadInfo")
    public ResponseResult addUploadInfo(Slide slide) {

        return slideService.addUploadInfo(slide);

    }

    // 获取图片资源
    @GetMapping("front/getSlidePicture")
    public void getSlidePicture(String uuid, HttpServletResponse response) {


        try {
            // 1.  根据uuid加载本地的图片资源
            FileInputStream fileInputStream = new FileInputStream("D:/upload/slides/" + uuid);
            // 2.通过响应对象 获取输出流
            ServletOutputStream outputStream = response.getOutputStream();
            // 3. 实现流的拷贝
            IOUtils.copy(fileInputStream,outputStream);

            // 释放资源
            fileInputStream.close();
            outputStream.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

    // 图片置顶  update
    @PostMapping("update")
    public ResponseResult topPicture(Integer id) {

       return slideService.topPicture(id);

    }


    // 删除图片
    @PostMapping("delete")
    public ResponseResult deletePicture(String uuid){

       return slideService.deletePicture(uuid);

    }


    // 门户网站轮播图片列表加载  /front/list
    @GetMapping("front/list")
    public ResponseResult pictureList(Integer limit){

        return slideService.pictureList(limit);

    }


}
